package com.camera2.http;

import android.content.Context;
import android.util.Log;

import com.camera2.Utils.StreamUtils;
import com.camera2.bean.ApplicationHelper;
import com.camera2.bean.ExpertBean;
import com.camera2.bean.InjuryBean;
import com.camera2.local.SharedPrefer;
import com.google.gson.Gson;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SendExpert {

   private final static  String TAG = "SendExpert";
    private static Context context;
    private static int code;
    private static String msg;
    private static String url;

    public static String send(Context context,String saveUpdate)
            throws Exception {
        String token = SharedPrefer.ReadToken(context);
        ExpertBean bean = ((ApplicationHelper) context.getApplicationContext().getApplicationContext()).getExpertBean();
        HttpClient client = new DefaultHttpClient();
        if(saveUpdate.equals("save")){
            url=GlobalUrl.getSaveExpertUrl();
        }else{
            url=GlobalUrl.getUpdateExpertUrl();
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("token", token);
        httpPost.addHeader("Content-Type", "application/json");
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("id", bean.getId() + "");
        jsonParam.put("userId", bean.getUserId());
        jsonParam.put("price", bean.getPrice());
        jsonParam.put("phone", bean.getPhone());
        jsonParam.put("name", bean.getName());
        jsonParam.put("introduce", bean.getIntroduce());


        StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");//解决中文乱码问题
        entity.setContentEncoding("UTF-8");
        entity.setContentType("application/json");
        httpPost.setEntity(entity);

        HttpResponse response = client.execute(httpPost);
        int code = response.getStatusLine().getStatusCode();
        InputStream is = response.getEntity().getContent();
        String result = StreamUtils.readStream(is);
        Log.i(TAG, "UpdatePassword: " + result);
        JSONObject jsonObj = new JSONObject(result);
        String msg = jsonObj.getString("msg");
        return msg;
    }
    public static String getExpert(Context context1) throws Exception {
        context = context1;
        String token = SharedPrefer.ReadToken(context);
        String user_id = SharedPrefer.Readid(context);
        OkHttpClient mOkHttpClient = new OkHttpClient();
        FormBody body = new FormBody.Builder().build();
        Request request = new Request.Builder()
                .url(GlobalUrl.getExpertInfoUrl() + user_id)
                .addHeader("token", token)
                .post(body)
                .build();

        Call call = mOkHttpClient.newCall(request);
        try {
            Response response = call.execute();
            String result = (String) response.body().string();
            Log.i("getExpert", "result: " + result);
            JSONObject jsonObj = new JSONObject(result);
            code = Integer.parseInt(jsonObj.getString("code"));
            msg = jsonObj.getString("msg");
            String expert = jsonObj.getString("expert");
            if (code == 0) {
                Gson gs = new Gson();
                ExpertBean bean = gs.fromJson(expert, ExpertBean.class);
                ((ApplicationHelper) context1.getApplicationContext()).setExpertBean(bean);
                ExpertBean aa = ((ApplicationHelper) context1.getApplicationContext()).getExpertBean();
                Log.i("getExpert", "List<ExpertBean>: " + aa);
            } else return msg;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return msg;
    }
}
